package jeons.admin.product.biz.impl;

import java.util.Iterator;
import java.util.List;

import jeons.admin.product.biz.MemoryBiz;
import jeons.admin.product.biz.ProductBiz;
import jeons.admin.product.biz.ProductImgBiz;
import jeons.admin.product.dto.MemoryDTO;
import jeons.admin.product.dto.ProductDTO;
import jeons.admin.product.dto.ProductImgDTO;
import jeons.common.util.FileService;
import jeons.common.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional
public class MemoryBizImpl extends SqlMapClientDaoSupport implements MemoryBiz{
	@Autowired SqlMapClientTemplate sqlMapClientTemplate;
	@Autowired FileService fileService;
	@Autowired ProductImgBiz productImgBiz;
	@Autowired ProductBiz productBiz;
	
	@Override
	public MemoryDTO insertData(MemoryDTO memoryDTO, List listimg, List dteilimg) {
			ProductDTO productDTO = new ProductDTO();
			productDTO.setPrice(memoryDTO.getPrice());
			productDTO.setType(memoryDTO.getType());
			productDTO.setP_number(memoryDTO.getP_number());
			productDTO.setProduct(memoryDTO.getProduct());
			productDTO.setRgdate(memoryDTO.getRgdate());
			productDTO.setManufacturer(memoryDTO.getManufacturer());
			
			sqlMapClientTemplate.insert("Product.insertData",productDTO);
			memoryDTO.setProno(productDTO.getProno());
			sqlMapClientTemplate.insert("Memory.insertData",memoryDTO);
			String today = Utils.getYmdhhmmss("yyyyMMddhhmmss");
			ProductImgDTO productImgDTO = new ProductImgDTO();
			for (Iterator iterator = dteilimg.iterator();  iterator.hasNext();) {
				MultipartFile multipartFile = (MultipartFile)iterator.next();
				fileService.uploadData(multipartFile,"product",today);
				productImgDTO.setProno(productDTO.getProno());		// ��ǰ���̺� �ڵ�
				productImgDTO.setType(productDTO.getType());
				productImgDTO.setB_filename(today+multipartFile.getOriginalFilename());
				productImgDTO.setB_realname(multipartFile.getOriginalFilename());
				productImgDTO.setB_filesize((int)multipartFile.getSize());
				//System.out.println("listimg :"+multipartFile.getOriginalFilename());;
				
				
			}	
			for (Iterator iterator =  listimg.iterator();  iterator.hasNext();) {
				MultipartFile multipartFile = (MultipartFile)iterator.next();
				fileService.uploadData(multipartFile,"product",today);
				productImgDTO.setL_filename(today+multipartFile.getOriginalFilename());
				productImgDTO.setL_realname(multipartFile.getOriginalFilename());
				productImgDTO.setL_filesize((int)multipartFile.getSize());
				
			}
			productImgBiz.uploadImgData(productImgDTO);
		
		return memoryDTO;
	}

	@Override
	public MemoryDTO getData(int prono) {
		// TODO Auto-generated method stub
		return (MemoryDTO) sqlMapClientTemplate.queryForObject("Memory.getData",prono);
	}

	@Override
	public void updateData(MemoryDTO memoryDTO) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setPrice(memoryDTO.getPrice());
		productDTO.setType(memoryDTO.getType());
		productDTO.setP_number(memoryDTO.getP_number());
		productDTO.setProduct(memoryDTO.getProduct());
		productDTO.setRgdate(memoryDTO.getRgdate());
		productDTO.setProno(memoryDTO.getProno());
		productDTO.setManufacturer(memoryDTO.getManufacturer());
		productBiz.updateData(productDTO);
		sqlMapClientTemplate.insert("Memory.updateData",memoryDTO);
		
	}

	@Override
	public MemoryDTO getALLData(int prono) {
		return (MemoryDTO) sqlMapClientTemplate.queryForObject("Memory.getALLData", prono);
	}

}
